package com.an.blog.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.an.blog.admin.domain.bo.BlogDailyImageBo;
import com.an.blog.admin.domain.vo.BlogDailyImageVo;
import com.an.blog.third.job.BlogJobService;
import com.an.blog.service.IBlogDailyImageService;
import com.an.common.annotation.Log;
import com.an.common.annotation.RepeatSubmit;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.core.validate.AddGroup;
import com.an.common.core.validate.EditGroup;
import com.an.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 博客每日图片
 *
 * @author Ann
 * @date 2023-09-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/blog/dailyImage")
public class BlogDailyImageController extends BaseController {

    private final IBlogDailyImageService blogDailyImageService;
    private final BlogJobService blogJobService;

    /**
     * 查询博客每日图片列表
     */
    @SaCheckPermission("blog:dailyImage:list")
    @GetMapping("/list")
    public TableDataInfo<BlogDailyImageVo> list(BlogDailyImageBo bo, PageQuery pageQuery) {
        return blogDailyImageService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取博客每日图片详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("blog:dailyImage:query")
    @GetMapping("/{id}")
    public R<BlogDailyImageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(blogDailyImageService.queryById(id));
    }

    /**
     * 新增博客每日图片
     */
    @SaCheckPermission("blog:dailyImage:add")
    @Log(title = "博客每日图片", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogDailyImageBo bo) {
        return toAjax(blogDailyImageService.insertByBo(bo));
    }

    /**
     * 修改博客每日图片
     */
    @SaCheckPermission("blog:dailyImage:edit")
    @Log(title = "博客每日图片", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogDailyImageBo bo) {
        return toAjax(blogDailyImageService.updateByBo(bo));
    }


    /**
     * 改变状态
     *
     * @param bo 薄
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/09/25 10:40:10
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:article:edit")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody BlogDailyImageBo bo) {
        return toAjax(blogDailyImageService.updateDailyImageStatus(bo));
    }

    /**
     * 删除博客每日图片
     *
     * @param ids 主键串
     */
    @SaCheckPermission("blog:dailyImage:remove")
    @Log(title = "博客每日图片", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(blogDailyImageService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 刷新
     *
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/10/28 14:25:28
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:dailyImage:edit")
    @Log(title = "博客每日图片", businessType = BusinessType.OTHER)
    @PostMapping("/refresh")
    public R<Void> refresh() {
        blogJobService.refreshImageCacheHandler();
        return R.ok();
    }
}
